﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>接受人选择器</title>
    <link href="../Scripts/bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/components-rounded.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/themes/default/style.min.css" rel="stylesheet" />
    <script type="text/javascript" src="../Scripts/bootstrap/js/jquery.min.js"></script>
    <script type="text/javascript" src="../Scripts/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../Scripts/commonYangYH.js"></script>
    <script src="../Scripts/QueryString2016.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/js/jquery.cokie.min.js" type="text/javascript" ></script>
    <script type="text/javascript" src="../Scripts/config.js" type="text/javascript" ></script>
    <script type="text/javascript" src="../Scripts/bootstrap/js/jstree.js"></script>
    <script src="../Comm/Gener.js" type="text/javascript"></script>
    <link href="../../DataUser/Style/ccbpm.css" rel="Stylesheet" />
    <!--
       杨玉慧.

    1. 初始化的标记是 ?DoType=Accepter_Init&FK_Node=xxxx&ToNode=0&WorkID=xxxxx.  参数: WorkID
       返回的是 节点列表 Nodes, 部门列表 Depts, 人员列表 Emps

    2, 用户选择一个节点，标记是 DoType=Accepter_Save&FK_Node=xxx&WorkID=xxx&ToNode=xxxx&SelectEmps=zhangsan,lisi.
       返回的是: 保存结果.
    -->
    <style type="text/css">
        * {
            list-style: none;
            text-align: left;
        }
        
        body .table tbody tr td {
            line-height: 18px;
        }

        .portlet-body,.portlet-title {
            background: #f4f9ff !important;
            border-bottom: 0px;
        }

        .customthr tr:nth-child(2n) {
            background: #d4e6fe;
        }

        .customthr tr:nth-child(2n+1) {
            background: #f4f9ff;
        }

        .customthr thead tr {
            background: #d4e6fe !important;
        }

        .btn {
            background: #2884fa !important;
            border-radius: 20px !important;
            color: white !important;
            padding: 4px 17px !important;
        }

        input[type=text], select {
            border-radius: 15px !important;
            border: #2884fa 1px solid !important;
            margin: 8px 0px !important;
            line-height: 25px;
            padding-left: 6px;
        }

        #btnSearch {
            height: 30px;
            border: none;
            border-radius: 20px;
            padding: 6px 12px;
            margin-bottom: 0;
            font-size: 14px;
            background: #2884fa !important;
            color: #fff;
        }

        #txbSearchVal {
            font-size: 14px;
        }
        /*#poptable {
                display: none;
            }*/
        #test {
        }

        .form_tree {
            background: #f4f9ff;
        }

            .form_tree #orgTxt {
                border-radius: 15px 0px 0px 15px !important;
                margin: 0px !important;
                /*border:#2884fa 1px solid;*/
            }

            .form_tree .input-group-addon {
                border-radius: 0px 15px 15px 0px !important;
                /*broder:#2884fa 1px solid;*/
            }

        #groupTable #dt ul {
            /*background: #f4f9ff;*/
            font-weight: normal;
            background: #d4e6fe;
        }

        #groupTable #dt > li {
            font-weight: bold;
        }
        #searchS {
            overflow:hidden;
        }
        #searchS div{
            height:40px;
            line-height:40px;
            text-align:left;
            float: left;
        }
    </style>
    <script type="text/javascript">

        var doType = GetQueryString("DoType");

        $(function () {

            if (doType == null || doType == "")
                doType = "Accepter_Save";

            doType = "Accepter_Save";
            if (doType == "Accepter_Send") {
                $("#btnOk").text('发送');
            } else {
                $("#btnOk").text('保存');
            }

            dtTable();

            initSelectEmpModal();

            $("#groupTable").height($(window).height() - 80 + "px");

        });
        //配置数据
        var firstLoad = true;
        var toNode;
        var pageUrlParam = { fk_node: GetQueryString("FK_Node"), WorkID: GetQueryString("WorkID"), FK_Flow: GetQueryString("FK_Flow"), toNode: GetQueryString("ToNode") != undefined ? GetQueryString("ToNode") : 0 };

        function dtTable() {

            if (pageUrlParam.toNode == 0) {
                if (firstLoad) {
                    toNode = 0;
                } else {
                    toNode = $("#searchS1 option:selected").val()
                }
            } else {
                $("#searchS").css('display', 'none');
                toNode = pageUrlParam.toNode;
            }

            $.ajax({
                type: 'post',
                async: true,
                url: Handler + "?DoType=Accepter_Init&FK_Node=" + pageUrlParam.fk_node + '&WorkID=' +
                    pageUrlParam.WorkID + '&m=' + Math.random(),
                dataType: 'html',
                data: { ToNode: toNode },
                success: function (data) {

                    if (data.indexOf('err@') == 0) {
                        alert(data);
                        return;
                    }

                    var initData = {};
                    if (data == null && data == undefined) {
                        alert("Accepter_Init&FK_Node=" + pageUrlParam.fk_node + '&WorkID=' +
                        pageUrlParam.WorkID + "未获取到数据" + data);
                        return;
                    }

                    try {
                        initData = eval('(' + data + ')');
                    }
                    catch (err) {
                        alert("转换JSON 失败" + err + data);
                        return;
                    }

                    var selDataArr = initData.Nodes;
                    var dtgArr = initData.Depts;
                    var dteArr = initData.Emps;

                    //增加了: 默认选择接收人，与强制选择接收人的数据源,让其在打开的时候，可以把默认值选择上.
                    // 有可能值是空的.
                    var defaultSelected = initData.DefaultSelected;
                    var forceSelected = initData.ForceSelected;

                    //初始化到达节点数据
                    if (firstLoad == true) {
                        firstLoad = false;
                        var html = '';

                        $.each(selDataArr, function (i, obje) {
                            html += '<option value="' + obje.No + '">' + obje.Name + '</option>';
                        });

                        $("#searchS1").html("");
                        $("#searchS1").html(html);

                        if (pageUrlParam.toNode == 0) {
                            $("#searchS1 option:nth(0)").attr("selected", "selected");
                        } else {
                            $("#searchS1 option[value=" + pageUrlParam.toNode + "]").attr("selected", "selected");
                        }
                    }

                    var selectNodeVal = $('#searchS1').val();
                    var selectNode = $.grep(selDataArr, function (value) {
                        return value.NO == selectNodeVal;
                    });

                    //通过SQL 和树的时候可以显示为树.
                    if (selectNode[0].SelectorDBShowWay == "Tree" && selectNode[0].SelectorModel == "SQL") {
                        //树展示
                        $('#jstree').show();
                        var jsTreedata = [];
                        $.each(dtgArr, function (i, obj) {
                            //jsTreedata.push({ "id": obj.No, "parent": (obj.ParentNo == "0" ? '#' : obj.ParentNo), "text": obj.Name,"data":obj });
                            // 2016-12-05 by tianbaoyan 人员选择器默认展开  因为数据量通过sql过滤不是很大
                            jsTreedata.push({ "id": obj.No, "parent": (obj.ParentNo == "0" ? '#' : obj.ParentNo), "text": obj.Name, "data": obj, state: { opened: true} });
                        });

                        $.each(dteArr, function (i, obj) {
                            var disabled = false;
                            var selected = false;
                            //默认勾选
                            if (defaultSelected) {
                                for (var j = 0; j < defaultSelected.length; j++) {
                                    if (obj.NO == defaultSelected[j].FK_Emp) {
                                        selected = true;
                                        break;
                                    }
                                }
                            }
                            //强制勾选
                            if (forceSelected) {
                                for (var k = 0; k < forceSelected.length; k++) {
                                    if (obj.NO == forceSelected[k].FK_Emp) {
                                        selected = true;
                                        disabled = true;
                                        break;
                                    }
                                }
                            }
                            jsTreedata.push({ children: {}, "id": "user" + obj.No, "parent": obj.FK_Dept, "text": obj.Name, "icon": "jstree-file", "data": obj, "state": { "disabled": disabled, "selected": selected} });
                        });

                        //没有父节点的给到根节点
                        for (var i = 0; i < jsTreedata.length; i++) {
                            if (jsTreedata[i].parent != '#') {
                                var pNode = $.grep(jsTreedata, function (value) {
                                    return value.id == jsTreedata[i].parent;
                                });
                                if (pNode.length == 0) {
                                    jsTreedata[i].parent = '#'
                                }
                            }
                        }

                        $('#jstree').jstree({
                            plugins: [
                            "checkbox",
                            //"state",
                            //"conditionalselect"
                            ],
                            'core': {
                                "multiple": true,
                                'data': jsTreedata
                            }
                        })
                        $(".wait").hide();
                        $("section").show();
                        return;
                    }
                    if (selectNode[0].SELECTORMODEL == "GenerUserSelecter") {//通用人员选择
                        $('#selectEmpsDiv').css('display', 'block');
                        $('#groupTable').css('display', 'none');

                        $(".wait").hide();
                        $("section").show();
                        return;
                    } else {//其它均显示成分组
                        $('#groupTable').css('display', 'block');
                        $('#selectEmpsDiv').css('display', 'none');
                    }
                    window.fkName = '';
                    if (dteArr != undefined && dteArr.length > 0) {
                        for (var pro in dteArr[0]) {
                            if (pro.toLowerCase() != "no" && pro.toLowerCase() != "name") {
                                fkName = pro;
                                break;
                            }
                        }
                    }
                    var ultdg = '';
                    var ultde = '';
                    var ultdes = '';
                    var s = "";
                    $.each(dtgArr, function (i, obje) {
                        for (var k = 0; k < dteArr.length; k++) {
                            if (obje.No == dteArr[k][fkName]) {
                                ultde +=
                                    "<li data-data='true' style='float:left;margin-right:10px;' class='lim'><input type='checkbox' style='margin: 5px 5px;'/>" + dteArr[k].Name + "</li>";
                            }
                        }
                        ultdg += "<li data-data='true' class='lif' name='" + obje.No + "'><input type='checkbox' class='ulm_head' style='margin: 5px 0px;'/>" + obje.Name + "<ul style='overflow:hidden;margin:10px 5px 10px 0px;'>" +
                                ultde + "</ul></li>";
                        ultde = "";
                    });
                    //
                    var groupFileds = $.grep(dteArr, function (value) {
                        return ($('[name="' + value.FK_Dept + '"]').length != "0");
                    });

                    $.each(groupFileds, function (j, objj) {
                        ultdes += "<li data-data='true' style='float:left;margin-right:10px;' class='lim'><input type='checkbox' style='margin: 5px 5px;'/>" + objj.Name + "</li>";
                    });
                    s = "<li class='lif'><input type='checkbox' class='ulm_head' style='margin: 5px 0px;'/>其他<ul style='overflow:hidden;margin:10px 5px 10px 0px;'>" +
                            ultdes + "</ul></li>";
                    ultdg += s;

                    //
                    $("#groupTable #dt").html(ultdg);
                    $(".wait").hide();
                    $("section").show();
                    $(".ulm_head").click(function () {
                        if (this.checked) {
                            $(this).parent("li").children("ul").find("input").prop("checked", "checked");
                        } else {
                            $(this).parent("li").children("ul").children("li").children("input").removeAttr("checked", "checked");
                        }
                    });
                    $(".ulm_head").click(function () {
                        if (this.checked) {
                            $(this).parent("li").children("ul").find("input").prop("checked", "checked");
                        } else {
                            $(this).parent("li").children("ul").children("li").children("input").removeAttr("checked", "checked");
                        }
                    });
                    //实体绑定
                    $.each(dteArr, function (k, obj) {
                        var lifs = $("#groupTable #dt .lif");
                        for (var i = 0; i < lifs.length; i++) {
                            if ($(lifs[i]).attr("name") == obj[fkName]) {
                                var limData = $(lifs[i]).find(".lim");
                                for (var m = 0; m < limData.length; m++) {
                                    var lim = limData[m];
                                    if ($(lim).text() == obj.NAME && $(lim) != undefined && $(lim).data != undefined && $(lim).data() != undefined && $(lim).data().data == true) {
                                        $(lim).data().data = obj;
                                        break;
                                    }
                                }
                            }
                        }
                    });
                }

            });
        }
        function getSelectEmps() {
            if ($('#groupTable').css('display') != 'none') { //分组
                var checked = $('input:checked');
                var selectedIdArrm = [];
                for (var i = 0; i < checked.parent(".lim").length; i++) {
                    selectedIdArrm.push($(checked.parent(".lim")[i]).data().data.NO);
                }
                return selectedIdArrm.join(';');
            } else if ($('#jstree').css('display') != 'none') {//树和BySql
                //获取树选中的值
                var instance = $('#jstree').jstree(true);
                var selectedIdArr = instance.get_selected();
                var selectedNodes = [];
                var nodeDataArrs = [];
                var noArr = [];
                $.each(selectedIdArr, function (i, selectedId) {
                    selectedNodes.push(instance.get_node(selectedId));
                });
                $.each(selectedNodes, function (i, selectedNode) {
                    if (selectedNodes[i].icon != true) {
                        nodeDataArrs.push(selectedNode.data);
                    }
                });

                $.each(nodeDataArrs, function (i, nodeData) {
                    noArr.push(nodeData.NO);
                });
                return noArr.join(';');
            } else {//通用人员选择器
                return frames["iframeSelectEmpsForm"].window.returnVal.NO;
                var checked = $('input:checked');
                var selectedIdArrm = [];
                for (var i = 0; i < checked.parent(".lim").length; i++) {
                    selectedIdArrm.push($(checked.parent(".lim")[i]).data().data.NO);
                }
                return selectedIdArrm.join(';');
            }
        }

        //确定【选择接收人并发送】 / 保存.  Accepter_Save Accepter_Send
        function SendNode() {

            //Accepter_Save&FK_Node=xxx&WorkID=xxx&ToNode=xxxx&SelectEmps=zhangsan,lisi

            var empNos = getSelectEmps();
            if (empNos == '' || empNos == null) {
                alert('请选择人员.');
                return;
            }
            
            if (pageUrlParam.toNode == 0) {
                toNode = $("#searchS1 option:selected").val();
            } else {
                toNode = pageUrlParam.toNode;
            }

            $.ajax({
                type: 'post',
                async: true,
                data: { "FK_Flow": pageUrlParam.FK_Flow, "WorkID": pageUrlParam.WorkID, ToNode: toNode, FK_Node: pageUrlParam.fk_node },
                url: Handler + "?DoType=" + doType + "&m=" + Math.random() + "&SelectEmps=" + encodeURI(empNos) + "&FK_Flow=" + pageUrlParam.FK_Flow + "&WorkID=" + pageUrlParam.WorkID,
                dataType: 'html',
                success: function (data) {
                    // alert(data);
                    if (data.indexOf('err@') == 0) {
                        alert(data);
                        return;
                    }

                    if ("Accepter_Save" == doType) {
                        if (window.parent != null && window.parent.returnWorkWindowClose != null) {
                            window.parent.returnWorkWindowClose(data);
                        } else if (window.opener != undefined && window.opener.returnWorkWindowClose != undefined) {
                            window.opener.returnWorkWindowClose(data);
                        } else if (window.parent != undefined && window.parent.AccepterHtmlSave != undefined) {
                            //表单树使用
                            window.parent.AccepterHtmlSave(data);
                        } else {
                            $('body').append($('<div>' + data + '</div>'));
                            $('#btnOk').attr('disabled', true);
                        }
                        //alert('保存成功...');
                        window.close();
                        return;
                    }


                    if (window.parent != null && window.parent.returnWorkWindowClose != null) {
                        window.parent.returnWorkWindowClose(data);
                    } else if (window.opener != undefined && window.opener.returnWorkWindowClose != undefined) {
                        window.opener.returnWorkWindowClose(data);
                    } else {
                        $('body').append($('<div>' + data + '</div>'));
                        $('#btnOk').attr('disabled', true);
                    }
                }
            });
            $(".foot").hide();
        }

        //取消【选择接收人并发送】
        function Close() {
            if (window.parent != null && window.parent.returnWorkWindowClose != null) {
                window.parent.returnWorkWindowClose("取消");

                window.close();
            } else if (window.opener != undefined && window.opener.returnWorkWindowClose != undefined) {
                window.opener.returnWorkWindowClose("取消");

                window.close();
            } else {
                window.close();
            }
        }
        //初始化人员选择窗口
        function initSelectEmpModal() {
            var selectEmpModalHtml = '<div class="modal fade" id="selectEmpsModal" style="width: 80%;margin: 40px auto;">' +
               '<div class="modal-dialog" style="margin: 0px;">'
                   + '<div class="modal-content" style="border-radius:0px;width:700px;">'
                      + '<div class="modal-header">'
                          + '<button type="button" class="close" style="color:white;opacity:1;" data-dismiss="modal" aria-hidden="true">&times;</button>'
                           + '<h4 class="modal-title">选择人员</h4>'
                       + '</div>'
                       + '<div class="modal-body">'
                           + '<iframe style="width:100%;border:0px;height:400px;" id="iframeSelectEmpsForm" name="iframeSelectEmpsForm"></iframe>'
                       + '</div>'
                   + '</div><!-- /.modal-content -->'
               + '</div><!-- /.modal-dialog -->'
           + '</div>';

            $('body').append($(selectEmpModalHtml));

            $('#BtnSelectEmps').bind('click', function () {


                var dept = getQueryStringByNameFromUrl("?" + $.cookie('CCS'), "FK_Dept");
                if (dept == null || dept == '' || dept == undefined) {
                    dept = $.cookie('FK_Dept');
                }
                if (dept == null || dept == '' || dept == undefined) {
                    var u = new WebUser();
                    dept = u.FK_Dept;
                }
                // 特殊判断天业集团的bug.
                if (dept == "101")
                    dept = "10102";

                var url = "SelectEmps.htm?FK_Dept=" + dept + "&FK_Node=" + GetQueryString("FK_Node") + "&s=" + Math.random() + "&IsSelectMore=1";
                alert(url);

                $('#iframeSelectEmpsForm').attr('src', url);
                $('#selectEmpsModal').modal().show();
            });
        }

        //设置选中的人员
        function selectEmpsWindowClose(data) {
            $('#selectEmpsModal').modal('hide');
            if (data == '取消') {
                return;
            } else {
                $('#ToEmp').val(frames["iframeSelectEmpsForm"].window.returnVal.NAME);
            }
        }
        </script>
</head>
<body  >
<div class="wait" style="text-align: center;">页面数据正在加载中，请稍候......</div>
<section style="display: none;">
    <div id="searchS">
        请选择要到达到的节点：<select id="searchS1" onclick="dtTable()" style="width: 76%;"></select>
    </div>
    <div id="groupTable" style="display: none; overflow-y: auto; text-align: left;">
        <ul id="dt" data-data="true" style="padding-left: 0px;"></ul>
    </div>
    <div id="jstree" style="display: none; overflow-y: auto; text-align: left;"></div>
    <div id="selectEmpsDiv" style="display: none;">
        <div style="margin: 20px auto;">
            请选择接受人：
            <input type="button" id="BtnSelectEmps" value='选择接受人' style="float: right; height: 27px;"/>
            <textarea id="ToEmp" name="ToEmp" cols="20"  rows="3" style="height: 120px; resize: none;"></textarea>
         </div>
    </div>
    <div style="position: fixed; bottom: 0px; right: 0px;" class="foot">
        <!--<button id="btnSaveAndClose" class="btn btn-info" onclick="SendNode('Accepter_Save')">确定</button>-->
        <button id="btnOk" class="btn btn-info" onclick="SendNode()" >发送</button>
        <button id="btnCancel" class="btn btn-info" onclick="Close()">取消</button>
    </div>
</section>
</body>
</html>
